package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import controller.IGameController;

public class GameWindow extends JFrame {
	/* Required for serializable interface */
	private static final long serialVersionUID = 1L;
	/* The canvas to draw on */
	private Canvas canvas;
	/* Controller to communicate with */
	IGameController gameController;

	/** Constructs a new game window */
	public GameWindow(IGameController gameController) {
		this.gameController = gameController;
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setSize(800, 600);
		init();
	}
	
	/** Constructs a new game window */
	public GameWindow(String title, IGameController gameController) {
		super(title);
		this.gameController = gameController;
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setSize(800, 600);
		init();
	}
	
	/** Initializes the different window components */
	public void init() {
		// TODO check names
		
		// Create menu bar
		JMenuBar menuBar = new JMenuBar();
		
		// Create game menu
		JMenu gameMenu = new JMenu("Game");
		menuBar.add(gameMenu);
		
		// New game
		JMenuItem newGameItem = new JMenuItem("New game");
		gameMenu.add(newGameItem);
		
		// High scores
		JMenuItem highScoresItem = new JMenuItem("Highscores");
		gameMenu.add(highScoresItem);
		
		// Undo last move
		JMenuItem undoLastMoveItem = new JMenuItem("Undo last move");
		undoLastMoveItem.setAccelerator(KeyStroke.getKeyStroke('U'));
		undoLastMoveItem.addActionListener(new UndoListener());
		gameMenu.add(undoLastMoveItem);
		
		// Quit
		JMenuItem quitItem = new JMenuItem("Quit");
		quitItem.addActionListener(new QuitListener());
		gameMenu.add(quitItem);
		
		// Create image menu
		JMenu viewMenu = new JMenu("View");
		menuBar.add(viewMenu);
		
		// Go full screen
		JMenuItem goFullScreenItem = new JMenuItem("Go full screen");
		goFullScreenItem.addActionListener(new FullScreenListener());
		viewMenu.add(goFullScreenItem);
		
		// Render images
		JMenuItem renderImagesItem = new JMenuItem("Render images");
		viewMenu.add(renderImagesItem);
		
		// Render shapes
		JMenuItem renderShapesItem = new JMenuItem("Render shapes");
		viewMenu.add(renderShapesItem);
		
		// Create help menu
		JMenu helpMenu = new JMenu("Help");
		menuBar.add(helpMenu);

		// Instructions
		JMenuItem instructionsItem = new JMenuItem("Instructions");
		helpMenu.add(instructionsItem);
		
		// About
		JMenuItem aboutItem = new JMenuItem("About");
		helpMenu.add(aboutItem);
		
		// Add menu bar to window
		this.setJMenuBar(menuBar);
		
		// Create canvas
		this.canvas = new Canvas(this.gameController.getContext());
		
		// Canvas should listen for mouse clicks
		this.canvas.addMouseListener(new CanvasClickedListener());
		
		// Add canvas to window
		this.add(this.canvas);
	}
	
	/** Listens for clicks on the canvas */
	private class CanvasClickedListener extends MouseAdapter {
		public void mouseReleased(MouseEvent e) {
			gameController.handleMouseClick(e);
		}
	}
	
	/** Listens for undo requests */
	private class UndoListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.out.println("Made a silly move?");
			//controller.undoLastMove();
		}
	}
	
	/** Listens for quit requests */
	private class QuitListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.out.println("You ain't going nowhere!");
			//gameController.quit();
		}
	}
	
	/** Listens for full screen requests */
	private class FullScreenListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			gameController.runFullScreen();
		}
	}
}